﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using CommonsLib.Settings;
using CommonsLib.Utility;
using System.Data;

using System.Security.Cryptography;
using MaxLeaf.Platform.Functions;
using MaxLeaf.Platform.Commons;

namespace MaxLeaf.Platform.DALImplementation
{
    public class poOrderRecvHdrDAL : BaseDAL
    {
        
        
        public void SetPosted(CommonsLib.DB.DBAccess dbcon, int intHdrID)
        {
            string strSql = @"update poOrderRecvHdr set posted=1  where FInterID=@FInterID and posted=0 
                and not exists (select hdrid from poOrderRecvDtl where HdrID=@FInterID and posted=0) ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intHdrID.ToString())
 
            });
        }


        public bool WritePHPOrderToSCPHdr(DBAccess dbWebCon, DBAccess dbSCPCon, DBAccess dbSCPBase, poOrderRecvHdrDataModel poHdrModel, string strWebOrder_ID)
        {

            string strSQL = " Select A.* from order_main A inner join k3_order_main B on a.order_id=b.order_id ";
                  strSQL += " Where A.order_ID = " + strWebOrder_ID + " and B.ShipStatus='2' and B.paystatus='0'";
            using (DbDataReader drWebOrder = dbWebCon.GetDataReader(strSQL))
            {
                string strHdrFinterID = CvtFunc.ToString ( dbSCPCon.GetValue("select finterid from where poOrderRecvHdr where webordno='"+ CvtFunc.ToString (drWebOrder["Order_sn"]) +"'"));
                if (strHdrFinterID != "" || strHdrFinterID =="0")
                {
                    strSQL = "Delete from poOrderRecvHdr where FinterID='" + strHdrFinterID + "'";
                    dbSCPCon.ExecuteSql(strSQL); 
                    strSQL = " Delete from poOrderRecvDtl where hdrid='" + strHdrFinterID +"'";
                    dbSCPCon.ExecuteSql(strSQL);
                }
             
                if (drWebOrder.Read())
                {
                    if (CvtFunc.ToInt(drWebOrder["son_id"]) == 0)
                    {
                        poHdrModel.CompanyId = CvtFunc.ToInt(dbSCPBase.GetValue("select companyid from mCompany where WebUserID='" + drWebOrder["User_ID"] + "'"));
                    }
                    else
                    {
                        poHdrModel.CompanyId = CvtFunc.ToInt(dbSCPBase.GetValue("select companyid from mCompany where WebUserID='" + drWebOrder["son_id"] + "'"));
                    }
                    poHdrModel.WebOrdNo = CvtFunc.ToString(drWebOrder["Order_sn"]);
                    poHdrModel.OrdDate = CvtFunc.ToDateTime(drWebOrder["order_time"]);
                    poHdrModel.Phone = CvtFunc.ToString(drWebOrder["tel"]);
                    poHdrModel.Mobile = CvtFunc.ToString(drWebOrder["Mobile"]);
                    poHdrModel.PostalCode = CvtFunc.ToString(drWebOrder["zip"]);
                    poHdrModel.MDCId = CvtFunc.ToInt(drWebOrder["MDC_ID"]);
                    poHdrModel.TotAmt = CvtFunc.ToDouble(drWebOrder["order_amount"]) + CvtFunc.ToDouble(drWebOrder["ship_fee"]) ; 
                 }
            }
            return true ;
        }

        public void SetUnPosted(DBAccess dbcon, int intHdrID)
        {
            string strSql = @"update poOrderRecvHdr set posted=0  where FInterID=@FInterID and posted=1 
                and   exists (select hdrid from poOrderRecvDtl where HdrID=@FInterID and posted=0  and is_delete=0) ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intHdrID.ToString())
 
            });
        }
    }
}
